<?php


namespace app\utils;


class Image
{
    public static function urlToImgSrcData($url)
    {
        return "data:image/jpeg;base64," . base64_encode(file_get_contents($url));
    }

    public static function imgSrcData($imgData, $type = 'png')
    {
        return "data:image/{$type};base64," . base64_encode($imgData);
    }

    public static function cropImg($imgData, $width, $height, $x, $y)
    {
        $img = new \Imagick();
        $img->readImageBlob($imgData);
        $img->cropImage($width, $height, $x, $y);
        return $img->getImageBlob();
    }

    public static function isHorizontal($filepath)
    {
        $watermarkSizeInfo = getimagesize($filepath);
        if ($watermarkSizeInfo[0] >= $watermarkSizeInfo[1]) {
            return true;
        } else {
            return false;
        }
    }


    public static function imageSizeInfo($filepath)
    {
        $watermarkSizeInfo = getimagesize($filepath);
        return ['width' => $watermarkSizeInfo[0], 'height' => $watermarkSizeInfo[1]];
    }

}
